//
//  FindTypeCell.m
//  WOQIEarjoy
//
//  Created by 孙鹏飞 on 2023/11/24.
//  Copyright © 2023 WOQI. All rights reserved.
//

#import "FindTypeCell.h"

#import <BIToolModule/BIProjectDefine.h>

#import <SDWebImage/SDWebImage.h>

@interface FindTypeCell()

@property(nonatomic,strong) NSMutableArray * dataSource;
@property(nonatomic,strong) UIImageView * imagV ;
@property(nonatomic,strong) UILabel * titleLab ;
@property(nonatomic,strong) UIButton * btn ;

@end


@implementation FindTypeCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    _imagV = [[UIImageView alloc]init];
    _imagV.layer.cornerRadius = ((BIScreenWidth - 100)/4-20)/2;
    _imagV.layer.masksToBounds = YES;
//        _imagV.backgroundColor = [UIColor clearColor];
    _imagV .frame = CGRectMake(10, 20, (BIScreenWidth - 100)/4-20,  (BIScreenWidth - 100)/4-20);
//    _imagV .image = [UIImage imageNamed:@"ic／16／next"];
    [self.contentView addSubview:_imagV];
    
    _titleLab = [[UILabel alloc]init];
    _titleLab.frame = CGRectMake(0,90,(BIScreenWidth - 100)/4, 20);
//    _titleLab.text = @"右";
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.font = [UIFont systemFontOfSize:16];
    _titleLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:_titleLab];
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(0, 0,(BIScreenWidth - 100)/4, 120);
    _btn.backgroundColor = [UIColor clearColor];
    [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btn];
    
}
-(void)setModel:(BannerModel *)model{
    _model = model;
    [self.imagV sd_setImageWithURL:[NSURL URLWithString:model.image]];
    _titleLab.text = _model.title;
    
 
}
-(void)btnClick{
    if (_model.redirectPath) {
        [self jumpUrl:_model.redirectPath];
    }  
    
    NSDictionary * dic = @{@"ids":_model.ids};
//    [MobClick event:@"click_find_icon" attributes:dic];
}
-(void)jumpUrl:(NSString *)url{
    if ([url rangeOfString:@"jd.com"].location != NSNotFound) {
        
        NSString * jdUrl =[[NSString stringWithFormat:@"openapp.jdmobile://virtual?params={\"category\":\"jump\",\"des\":\"m\",\"url\":\"%@\",\"keplerID\":\"0\",\"keplerFrom\":\"1\",\"kepler_param\":{\"source\":\"kepler-open\",\"otherData\":{\"mopenbp7\":\"0\"}},\"union_open\":\"union_cps\"}",url] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"openapp.jdmobile://"]]) {
//            NSLog(@"安装了京东");
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:jdUrl] options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }else if ([url rangeOfString:@"taobao.com"].location != NSNotFound) {
        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tbopen"];
        NSURL *tbUrl = [NSURL URLWithString:str];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tbopen://"]]) {
            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }
//    else if ([url rangeOfString:@"tmall.com"].location != NSNotFound) {
//        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tmall"];
//        NSURL *tbUrl = [NSURL URLWithString:str];
//        if ([[UIApplication sharedApplication] canOpenURL:tbUrl]) {
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }else{
//            //直接跳转html
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }
//    }
    else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
    }
}
@end
